package com.rimi.controller;

import com.rimi.bean.Menu;
import com.rimi.bean.Page;
import com.rimi.bean.Video;
import com.rimi.service.MenuService;
import com.rimi.service.MenuTypeService;
import com.rimi.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("searchpage")
public class SearchPageController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private VideoService videoService;

    @RequestMapping("index")
    public String getIndex(Model model,HttpServletRequest request) {
        //把导航菜单列表存入Menu
        List<Menu> menuList = menuService.getMenuList();
        model.addAttribute("Menu", menuList);
        //从ListPage 获取到搜索框的输入内容
        String videoName = request.getParameter("videoName");
        model.addAttribute("videoName", videoName);

        //获取当前页
        String nowPage = request.getParameter("nowPage");
        //获取模糊查询条数
        int selectCount = videoService.selectCount(videoName);
        Page page=new Page();
        //获取尾页
        page.setCount(selectCount);
        int totalPage = page.getTotalPage();
        int now=0;
        if (nowPage!=null&&!"".equals(nowPage)){
            now = Integer.parseInt(nowPage);
            if(now<1) {
                now=1;
            }
            if(now>totalPage) {
                now=totalPage;
            }
            page.setNowPage(now);
        }
        int a=page.getNowPage()-1;
        if (a<0){
            a=a*-1;
        }
        int num1=a*page.getPageSize();
        //获取分页查询对象
        List<Video> list = videoService.selectLike(videoName,num1);
        model.addAttribute("VideoImage", list);

        //获取相关视频List
        List<Video> list1 = videoService.getVideoList();
        model.addAttribute("Videos", list1);
        model.addAttribute("page", page);


        return "search_page";
    }







}
